package com.taotao.portal.service.impl;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.HttpClientUtil;
import com.taotao.common.utils.JsonUtils;
import com.taotao.pojo.TbContent;
import com.taotao.portal.service.ContentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Package com.taotao.portal.service.impl
 * @Description:
 * @Author elwyn
 * @Date 2017/9/4 23:16
 * @Email elonyong@163.com
 */
@Service
public class ContentServiceImpl implements ContentService {
    @Value("${REST_BASE_URL}")
    private String REST_BASE_URL;
    @Value("${REST_INDEX_AD_URL}")
    private String REST_INDEX_AD_URL;

    @Override
    public String getContentList() {
        String s = HttpClientUtil.doGet(REST_BASE_URL + REST_INDEX_AD_URL);
        try {
            TaotaoResult taotaoResult = TaotaoResult.formatToList(s, TbContent.class);
            List<TbContent> data = (List) taotaoResult.getData();
            ArrayList<Map<String, Object>> objects = new ArrayList<>();
            for (TbContent datum : data) {
                Map<String, Object> map = new HashMap();
                map.put("src", datum.getPic());
                map.put("height", 240);
                map.put("width", 670);
                map.put("srcB", datum.getPic2());
                map.put("widthB", 550);
                map.put("height", 240);
                map.put("href", datum.getUrl());
                map.put("alt", datum.getSubTitle());
                objects.add(map);
            }
            return JsonUtils.objectToJson(objects);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";

    }
}
